{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines']\n"
     ]
    }
   ],
   "source": [
    "print([d for d in dir(fp) if not d.startswith('_')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "46\n",
      "This is a test text.\n",
      "This is second test text.\n"
     ]
    }
   ],
   "source": [
    "path = 'D:\\\\123.txt'\n",
    "\n",
    "fp = open(path,'w')\n",
    "print(fp.write('This is a test text.\\nThis is second test text.'))\n",
    "\n",
    "\n",
    "fp = open(path,'r')\n",
    "print(fp.read())\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://dl.google.com/dl/android/aosp/shamu-ota-ngi77b-0ea2ff80.zip\n"
     ]
    }
   ],
   "source": [
    "with open(path,'r') as fp:\n",
    "    print(fp.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "UnicodeEncodeError",
     "evalue": "'cp950' codec can't encode character '\\u8fd9' in position 2: illegal multibyte sequence",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnicodeEncodeError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-c5f096576356>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mpath\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'D:\\\\123.txt'\u001b[0m   \u001b[1;31m#找到这个文本文件\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mfp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'a'\u001b[0m\u001b[1;33m)\u001b[0m   \u001b[1;31m#双击打开文件,mode='w'表示写入文件\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'\\n这是一段中文'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m     \u001b[1;31m#write()方法写入文件内容\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m     \u001b[1;31m#写入完毕，close()方法关闭文件\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mUnicodeEncodeError\u001b[0m: 'cp950' codec can't encode character '\\u8fd9' in position 2: illegal multibyte sequence"
     ]
    }
   ],
   "source": [
    "path = 'D:\\\\123.txt'   #找到这个文本文件\n",
    "fp = open(path,'a')   #双击打开文件,mode='w'表示写入文件\n",
    "print(fp.write('\\n这是一段中文'))     #write()方法写入文件内容\n",
    "fp.close()     #写入完毕，close()方法关闭文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "fp = open(path,'a')\n",
    "print(fp.read())\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = 'D:\\\\123.txt'   #找到这个文本文件\n",
    "fp = open(path,'ab')   #双击打开文件,mode='b'表示二进制格式\n",
    "fp.write((os.linesep+'这是一段中文').encode('UTF-8'))     #write()方法写入文件内容,encode把中文为unicode编码\n",
    "fp.close()     #写入完毕，close()方法关闭文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "ename": "UnicodeDecodeError",
     "evalue": "'cp950' codec can't decode byte 0x80 in position 4: illegal multibyte sequence",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-46-f2929c5b88e0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mfp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'rt'\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m#二进制格式读取\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m#需要使用decode()解码为我们认识的语言\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m: 'cp950' codec can't decode byte 0x80 in position 4: illegal multibyte sequence"
     ]
    }
   ],
   "source": [
    "fp = open(path,'rb')  #二进制格式读取\n",
    "print(fp.read().decode())  #需要使用decode()解码为我们认识的语言"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-44-a1ee1f6663e5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlinesep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'str' object is not callable"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.linesep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "path = 'D:\\\\123.txt'  #找到这个文本文件\n",
    "linesep = os.linesep\n",
    "fp = open(path,'a+b')\n",
    "for i in range(10000):\n",
    "    fp.write(f'这是第 {i} 行写入的信息{linesep}'.encode('UTF-8'))\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "﻿这是第 0 行写入的信息\n"
     ]
    }
   ],
   "source": [
    "path = 'D:\\\\123.txt'\n",
    "fp = open(path,'rb')\n",
    "print(fp.read(33).decode())\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function readline:\n",
      "\n",
      "readline(size=-1, /) method of _io.BufferedReader instance\n",
      "    Read and return a line from the stream.\n",
      "    \n",
      "    If size is specified, at most size bytes will be read.\n",
      "    \n",
      "    The line terminator is always b'\\n' for binary files; for text\n",
      "    files, the newlines argument to open can be used to select the line\n",
      "    terminator(s) recognized.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(fp.readline)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "﻿这是第\n"
     ]
    }
   ],
   "source": [
    "path = 'D:\\\\123.txt'\n",
    "fp = open(path,'rb')\n",
    "print(fp.readline(12).decode())\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是第 20 行写入的信息\r\n",
      "\n",
      "这是第 21 行写入的信息\r\n",
      "\n",
      "这是第 22 行写入的信息\r\n",
      "\n",
      "这是第 23 行写入的信息\r\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(fp.readline().decode())\n",
    "print(fp.readline().decode())\n",
    "print(fp.readline().decode())\n",
    "print(fp.readline().decode())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是第 2 行写入的信息\r\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(fp.readline().decode())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function readlines:\n",
      "\n",
      "readlines(hint=-1, /) method of _io.BufferedReader instance\n",
      "    Return a list of lines from the stream.\n",
      "    \n",
      "    hint can be specified to control the number of lines read: no more\n",
      "    lines will be read if the total size (in bytes/characters) of all\n",
      "    lines so far exceeds hint.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(fp.readlines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "﻿这是第 0 行写入的信息\r\n",
      "这是第 1 行写入的信息\r\n",
      "这是第 2 行写入的信息\r\n",
      "这是第 3 行写入的信息\r\n"
     ]
    }
   ],
   "source": [
    "fp = open(path,'rb')\n",
    "for line in fp.readlines(99):\n",
    "    print(line.decode(),end='')\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
